<template>
  <div>
    <el-dialog
      :title="title"
      :closeOnClickModal="false"
      :visible.sync="show"
      width="600px">
      <el-form 
        label-position="right" 
        :model="selectTable" 
        ref="addForm" 
        label-width="80px" 
        autocomplete="off"
        size="medium">
        
        <el-form-item label="名称" prop="functionName" :rules="[rule[0]]">
            <el-input v-model="selectTable.functionName" placeholder="请输入功能名称" maxLength="20" style="width:200px"></el-input>
        </el-form-item>
        
        <el-form-item label="状态" prop="functionStatus" :rules="[rule[0]]">
          <el-switch
            v-model="selectTable.functionStatus"
            :inactive-value="1"
            :active-value="0"
            active-text="启用"
            inactive-text="停用"
            >
          </el-switch>
        </el-form-item>
        
        <el-form-item label="类型" prop="functionType" :rules="[rule[0]]">
          <el-switch
            v-model="selectTable.functionType"
            :inactive-value="1"
            :active-value="2"
            active-text="功能"
            inactive-text="页面"
            >
          </el-switch>
        </el-form-item>
        <template v-if="selectTable.functionType===2">
          <el-form-item label="功能码" prop="functionCode" :rules="[rule[0]]">
              <el-input v-model.trim="selectTable.functionCode" placeholder="请输入功能码" maxLength="50" style="width:300px"></el-input>
          </el-form-item>
        </template>
        <template v-else>
          <el-form-item label="路径Url" prop="functionUri">
              <el-input v-model.trim="selectTable.functionUri" placeholder="请输入路径Url" maxLength="50" style="width:300px"></el-input>
          </el-form-item>
          <el-form-item label="图标" prop="functionIcon">
            <iconlist v-model.trim="selectTable.functionIcon"></iconlist>
          </el-form-item>
        </template>
      </el-form>
      <div slot="footer" class="dialog-footer" v-loading="pload">
        <el-button @click="show = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('addForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { funcSave, funcModify } from '@/utils/api'
import Iconlist from '@/components/IconList'
export default {
  components: {
    Iconlist
  },
  props: {
    data: Object
  },
  created() {
    this.selectTable = this.JSONo({}, this.data)
  },
  watch: {
    show(newVal) {
      this.$parent.edit = false
    }
  },
  computed: {
    title() {
      if (this.selectTable.method === 'edit') {
        return '修改'
      } else if (this.selectTable.method === 'add') {
        return '添加'
      }
    }
  },
  data() {
    return {
      pload: false,
      rule: [
        { required: true, message: '必填项', trigger: 'blur' }
      ],
      show: true,
      selectTable: {}
    }
  },
  methods: {
    async submitForm(formName) {
      await this.$refs[formName].validate()
      try {
        this.pload = true
        if (this.selectTable.method === 'edit') {
          delete this.selectTable.children
          delete this.selectTable.parent
          await funcModify(this.selectTable)
        } else if (this.selectTable.method === 'add') {
          await funcSave(this.selectTable)
        }
        this.$notify({ title: '操作成功', type: 'success', duration: 5000 })
        this.$parent.edit = false
        this.$emit('getData')
      } catch (e) {
        this.pload = false
      }
    }
  }
}
</script>